package org.csu.mall.controller;

import org.csu.mall.common.CONSTANT;
import org.csu.mall.common.UniformResponse;
import org.csu.mall.entity.Product;
import org.csu.mall.service.IProductService;
import org.csu.mall.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/product/")
public class ProductController {
    @Autowired
    private IProductService productService;

    @GetMapping("detail")
    @ResponseBody
    public UniformResponse<ProductVO> getProductDetail(Integer productId, HttpSession session){
        UniformResponse<ProductVO> response=productService.getProductDetail(productId);
        if(response.isSuccess())
        {
            session.setAttribute(CONSTANT.PRODUCTINFO,response.getData());
        }
        return response;
    }

    @GetMapping("search")
    @ResponseBody
    public UniformResponse<List<Product>> searchProduct(String info)
    {
        return productService.searchByText(info);
    }

    @GetMapping("list")
    @ResponseBody
    public UniformResponse productList(
            @RequestParam(name="categoryId",required =false) Integer categoryId,
            @RequestParam(name="keyword",required =false) String keyword,
            @RequestParam(name="pageNum",defaultValue ="1") int pageNum,
            @RequestParam(name="pageSize",defaultValue ="5") int pageSize,
            @RequestParam(name="oderBy",defaultValue ="") String orderBy,
            HttpSession session) {
        UniformResponse response=productService.productList(categoryId,keyword,pageNum,pageSize,orderBy);
        if(response.isSuccess())
        {
            session.setAttribute(CONSTANT.PRODUCTLIST,response.getData());
        }

        return response;
    }


    @GetMapping("get_products")
    @ResponseBody
    public UniformResponse getProducts(
                                       @RequestParam(name="pageNum",defaultValue ="1") int pageNum,
                                       @RequestParam(name="pageSize",defaultValue ="5") int pageSize,HttpSession session)
    {
        UniformResponse response=productService.getProducts(pageNum, pageSize);
        if(response.isSuccess())
        {
            session.setAttribute(CONSTANT.PRODUCTS,response.getData());
        }
        return response;
    }

    @PostMapping("add_product")
    @ResponseBody
    public UniformResponse<Product> addProduct(Integer categoryId,
                                              @RequestParam(name="subTitle",required = false)String subTitle,
                                              @RequestParam(name="mainImage",required = false)String mainImage,
                                              @RequestParam(name="subImages",required = false)String subImages,
                                              @RequestParam(name="detail",required = false)String detail,
                                              BigDecimal price,
                                              Integer stock,
                                              @RequestParam(name="status" ,defaultValue = "1")Integer status,
                                              String name,
                                               HttpSession session) throws IOException {
        UniformResponse response = productService.addProduct(categoryId, subTitle, mainImage, subImages, detail, price, stock, status,name);

        if (response.isSuccess()) {
            session.setAttribute(CONSTANT.ADDPRODUCT, response.getData());
        }
        return response;
    }

    @PostMapping("delete_product")
    @ResponseBody
    public UniformResponse<String> deleteProduct(Integer productId){
        return productService.deleteProduct(productId);
    }

    @PostMapping("update_product")
    @ResponseBody
    public UniformResponse<Product> updateProduct( Integer productId,
                                                   @RequestParam(name="categoryId",required = false)Integer categoryId,
                                                 @RequestParam(name="subTitle",required = false)String subTitle,
                                                 @RequestParam(name="mainImages",required = false)String mainImage,
                                                 @RequestParam(name="subImages",required = false)String subImages,
                                                 @RequestParam(name="detail",required = false)String detail,
                                                  @RequestParam(name="price",required = false)BigDecimal price,
                                                  @RequestParam(name="stock",required = false)Integer stock,
                                                 @RequestParam(name="status" ,defaultValue = "1")Integer status,
                                                  @RequestParam(name="name",required = false)String name,
                                                  HttpSession session
                                                 ){
        UniformResponse response = productService.updateProduct(productId, categoryId, subTitle, mainImage, subImages, detail, price, stock, status, name);
        if (response.isSuccess()) {
            session.setAttribute(CONSTANT.UPDATEPRODUCT, response.getData());
        }
        return response;

    }

    @PostMapping("recommend_products")
    @ResponseBody
    public UniformResponse recommendProducts(String keyword,Integer pageNum,Integer pageSize,HttpSession session)
    {
        UniformResponse response = productService.recommendProduct(keyword, pageNum, pageSize);
        if (response.isSuccess()) {
            session.setAttribute(CONSTANT.RECOMMENDPRODUCTS, response.getData());
        }
        return response;

    }



}
